home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group93c.txt
/
000005_icon-group-sender _Tue Jul 6 16:43:02 1993.msg
< prev
next >
Wrap
Internet Message Format
|
1994-02-02
|
2KB
Received: from owl.CS.Arizona.EDU by cheltenham.CS.Arizona.EDU; Tue, 6 Jul 1993 15:47:57 MST
Received: by owl.cs.arizona.edu; Tue, 6 Jul 1993 15:47:56 MST
Date: Tue, 6 Jul 93 16:43:02 EDT
From: Paul_Abrahams@MTS.cc.Wayne.edu
To: icon-group@cs.arizona.edu
Message-Id: <696063@MTS.cc.Wayne.edu>
Subject: Transmitting values to co-expressions
Status: R
Errors-To: icon-group-errors@cs.arizona.edu
The section of the Icon book that discusses transmission of values to
co-expressions (p. 117) contains a non-obvious example of how
transmission works, and also comments that it's very hard and perhaps not
possible to find simple and well-motivated examples. Well, recently I
came upon a case where transmission of values to co-expressions is both
useful and (reasonably) transparent.
It's easy enough to program a recursive procedure for writing the
representation of an expression. Wherever there are some characters to
be generated, you just do "write" of those characters. But suppose,
instead, that you want to produce the =string image= of that expression.
You can, of course, pass values back up the recursive chain, but that is
less satisfying aesthetically. What you really want to do is to write
those values to a string, rather like the sprintf function in C.
Co-expressions provide a rather neat way of doing this. You create a
co-expression for generating the characters and then call it repeatedly,
each time concatenating its results onto a string. Within the generator,
each time characters are to be output, you pass them to @source. The
following program illustrates the method:
procedure main()
gen := create numbers()
result := ""
while result ||:= @gen
write(result)
end
procedure numbers()
every ((i := 1 to 10) || ",") @ &source
end
This program will set =result= to "1,2,3,4,5,6,7,8,9,10," and then write
it out. Although =numbers= is nonrecursive, it could just as well be
recursive. Note that this program does the same thing as the following
one:
procedure main()
numbers()
write()
end
procedure numbers()
every writes((i := 1 to 10) || ",")
end
Paul Abrahams
Reply-To: abrahams@acm.org